package com.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.common.utils.Result;
import com.entity.TaskStore;
import com.query.TaskStoreQuery;
import com.service.TaskStoreService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 任务仓库
 */
@CrossOrigin(allowCredentials = "true", allowedHeaders = "*")
@RestController
@RequestMapping("/taskStore")
public class TaskStoreController {
    @Autowired
    private TaskStoreService taskStoreService;
    /**
     * 添加任务
     */
    @RequestMapping("/addTaskStore")
    public Result addTaskStore(@RequestBody TaskStore taskStore){
        boolean flag = taskStoreService.insert(taskStore);
        if(flag){
            return Result.success();
        }else{
            return  Result.error("添加失败");
        }
    }

    /**
     * 根据id删除任务
     */
    @RequestMapping("/delTaskStore/{id}")
    public Result delTaskStore(@PathVariable Integer id){
        boolean flag= taskStoreService.deleteById(id);
        if(flag){
            return Result.success();
        }else{
            return  Result.error("删除失败");
        }
    }

    /**
     * 编辑任务
     */
    @RequestMapping("/editTaskStore")
    public Result editTaskStore(@RequestBody TaskStore taskStore){
        boolean flag= taskStoreService.updateById(taskStore);
        if(flag){
            return Result.success();
        }else{
            return  Result.error("编辑失败");
        }
    }


    /**
     * 根据习惯标签查询任务,和条件
     */
    @RequestMapping("/getTaskStoreByLabel")
    public Result getTaskStoreByLabel(@RequestBody TaskStoreQuery taskStoreQuery){
       return taskStoreService.getTaskStoreByLabel(taskStoreQuery);
    }

    /**
     *  根据条件查询任务/子任务  包括子任务id
     */
    @RequestMapping("/getSubTaskList")
    public Result getSubTaskList(@RequestBody TaskStoreQuery taskStoreQuery){
        return  taskStoreService.getSubTaskList(taskStoreQuery);
    }

    /**
     * 根据任务id查询详情
     */
    @RequestMapping("/getTaskById/{id}")
    public Result getTaskById(@PathVariable Integer id){
        Wrapper<TaskStore> wr=new EntityWrapper<>();
        wr.eq("id",id);
        TaskStore taskStore = taskStoreService.selectOne(wr);
        return  Result.success(taskStore);
    }

//    /**
//     * 更新任务数据
//     */
//    @RequestMapping("/updateTask")
//    public  Result updateTask(@RequestBody TaskStore taskStore){
//        boolean b = taskStoreService.updateById(taskStore);
//        if(b){
//            return  Result.success("更新成功");
//        }else{
//            return  Result.error("更新失败");
//        }
//    }

}
